<?php defined('SYSPATH') or die('No direct script access.');
 
class myfuns 
{
    public static function getip()
    {
    	if (isset($_SERVER)) 
		{ 
			if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) 
			{ 
				$realip = $_SERVER['HTTP_X_FORWARDED_FOR']; 
			} 
			elseif (isset($_SERVER['HTTP_CLIENT_IP'])) 
			{ 
				$realip = $_SERVER['HTTP_CLIENT_IP']; 
			}
			else
			{ 
				$realip = $_SERVER['REMOTE_ADDR']; 
			} 
		}
		else
		{ 
			if (getenv("HTTP_X_FORWARDED_FOR")) 
			{ 
				$realip = getenv( "HTTP_X_FORWARDED_FOR"); 
			}
			elseif (getenv("HTTP_CLIENT_IP")) 
			{ 
				$realip = getenv("HTTP_CLIENT_IP"); 
			}
			else
			{ 
				$realip = getenv("REMOTE_ADDR"); 
			} 
		} 

		return $realip; 
    }
	
	public static function genoption($name, $data, $selected=null)
	{
		$str = '<select name="'.$name.'">';
		
		foreach($data as $key=>$value)
		{
			$str .= '<option value="'.$key.'"';
			$str .= ($key == $selected);
		}
	}
	
	public static function sys_file_exists($f = NULL)
	{
		$h = array();
		$ret = FALSE;
		
		if(!is_null($f))
		{
			if(preg_match('/^http|https|ftp/',$f))
			{
				$h = @get_headers($f);
				
				if(array_key_exists(0,$h)) 
				{
					$ret = (bool) preg_match('/200|301/',$h[0]); /* HTTP/1.1 301 DAP (directory) */
				}
			}
			else
			{
				$ret = (file_exists($f) && is_readable($f));
			}
		}
		
		return (($ret === TRUE) ? TRUE : FALSE);
	}
	
    public static function move($original, $destination)
    {
    	if(!is_file($original)) return FALSE;
		
		if(self::create_folder(dirname($destination)))
		{
			if(file_exists($destination)) unlink($destination);
			copy($original, $destination);
			unlink($original);
		}
			
		return $destination;
    }
	
	public static function create_folder($folder)
	{
    	$folder = explode( "/" , $folder);
		$mkfolder = '';
		
		for(  $i=0 ; isset( $folder[$i] ) ; $i++ )
		{
			$mkfolder .= $folder[$i];
			
			if( $mkfolder != '' and ! is_dir( $mkfolder ) )
				mkdir( $mkfolder ,  0777);
				
			$mkfolder .= '/';
		}
		
		return TRUE;
	}

	public static function get_ups_shipping_fare($dest_postal, $dest_city, $dest_country, $service_type, $weight)
	{
		$country = new Country_Model($dest_country);
		
		// 计算运费
		$ups = Config::item('bookstore.nanhai');
		
		$Url = join("&", array("http://www.ups.com/using/services/rave/qcostcgi.cgi?accept_UPS_license_agreement=yes",
			"10_action=3",
			"13_product=".$service_type,
			"14_origCountry=".$ups['country'],
			"15_origPostal=".$ups['zipcode'],
			"origCity=".$ups['city'],
			"19_destPostal=".$dest_postal,
			"20_destCity=".$dest_city,
			"22_destCountry=".$country->iso_code_2,
			"23_weight=".round($weight<1 ? 1:$weight),
			"47_rateChart=Regular+Daily+Pickup",
			"48_container=00",
			"49_residential=01",
			"25_length=",
			"26_width=",
			"27_height="));
		
		$ResCode = '';

		if($Resp = fopen($Url, "r"))
		{
			while(!feof($Resp))
			{   
				$Result = fgets($Resp, 500);
				$Result = explode("%", $Result);
				$Err = substr($Result[0], -1);
				
				switch($Err)
				{
					case 3:
						$ResCode = $Result[8];
						break;
					case 4:
						$ResCode = $Result[8];
						break;
					case 5:
						$ResCode = $Result[1];
						break;
					case 6:
						$ResCode = $Result[1];
						break;
				}
			}
		
			fclose($Resp);
		}
		
		return $ResCode;
	}

}